package org.nightrunner.d3.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.search.Query;
import org.apache.lucene.search.BooleanClause.Occur;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.search.FullTextQuery;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.nightrunner.d3.entity.Category;
import org.nightrunner.d3.entity.Equipment;
import org.nightrunner.d3.model.IPaginationInfo;
import org.nightrunner.d3.model.PaginationModel;
import org.nightrunner.d3.service.ISearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.wltea.analyzer.lucene.IKQueryParser;

@Service
public class SearchService implements ISearchService {

	@Autowired
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Equipment> searchEquipment(List<String> keywords)
			throws IOException {
		List<Equipment> equipments = new ArrayList<Equipment>();
		Session session = sessionFactory.openSession();
		FullTextSession fullTextSession = Search.getFullTextSession(session);
		Transaction transaction = fullTextSession.beginTransaction();
		String[] fields = { "equipmentName" };
		String[] queries = new String[keywords.size()];
		Occur[] occrs = new Occur[keywords.size()];
		int i = 0;
		for (String query : keywords) {
			queries[i] = query;
			occrs[i] = Occur.MUST;
			i++;
		}
		Query query;
		query = IKQueryParser.parseMultiField(fields, queries, occrs);
		FullTextQuery fullTextQuery = fullTextSession
				.createFullTextQuery(query);
		List<Equipment> list = fullTextQuery.list();
		equipments.addAll(list);
		transaction.commit();
		session.close();
		return equipments;
	}

	@Override
	public List<Equipment> searchEquipments(String q) throws IOException {
		List<String> keywords = new ArrayList<String>(1);
		keywords.add(q);
		return searchEquipment(keywords);
	}

	@Override
	public PaginationModel<List<Equipment>> searchEquipments(
			List<String> keywords, IPaginationInfo paginationInfo)
			throws Exception {
		Session session = sessionFactory.openSession();
		FullTextSession fullTextSession = Search.getFullTextSession(session);
		Transaction transaction = fullTextSession.beginTransaction();
		String[] fields = { "equipmentName" };

		String[] queries = new String[keywords.size()];
		Occur[] occrs = new Occur[keywords.size()];
		int i = 0;
		for (String query : keywords) {
			queries[i] = query;
			occrs[i] = Occur.MUST;
			i++;
		}
		Query query = IKQueryParser.parseMultiField(fields, queries, occrs);
		FullTextQuery fullTextQuery = fullTextSession
				.createFullTextQuery(query);

		PaginationModel<List<Equipment>> paginationModel = new PaginationModel<List<Equipment>>();
		paginationModel.setPaginationInfo(paginationInfo);
		paginationModel.setHibernateQuery(fullTextQuery);

		transaction.commit();
		session.close();

		return paginationModel;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Category> searchCategorys(List<String> keywords)
			throws IOException {
		List<Category> equipments = new ArrayList<Category>();
		Session session = sessionFactory.openSession();
		FullTextSession fullTextSession = Search.getFullTextSession(session);
		Transaction transaction = fullTextSession.beginTransaction();
		String[] fields = { "categoryName" };
		String[] queries = new String[keywords.size()];
		Occur[] occrs = new Occur[keywords.size()];
		int i = 0;
		for (String query : keywords) {
			queries[i] = query;
			occrs[i] = Occur.MUST;
			i++;
		}
		Query query = IKQueryParser.parseMultiField(fields, queries, occrs);
		FullTextQuery fullTextQuery = fullTextSession
				.createFullTextQuery(query);
		List<Category> list = fullTextQuery.list();
		equipments.addAll(list);
		transaction.commit();
		session.close();
		return equipments;
	}

	@Override
	public List<Category> searchCategorys(String q) throws IOException {
		List<String> keywords = new ArrayList<String>(1);
		keywords.add(q);
		return searchCategorys(keywords);
	}
}
